﻿// -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SPContentTypeExts.cs
//  Developer:	Igor Solomatov
//  Created:	05.03.2013
// 
// -------------------- iSharePoint -------------------- //

using System;
using Microsoft.SharePoint;

namespace Smart.Common
{
	public static class SPContentTypeExts
	{
		public const string SystemPrefix = "0x";

		public static bool IsContentTypeId(this string value)
		{
			if (value.IsNullOrEmpty())
				return false;

			if (!value.StartsWith(SystemPrefix))
				return false;

			return
				value.SkipI(StringExts.Hex, 2, value.Length - 1) == -1;
		}

		public static bool IsParentContentTypeOf(this string parentContentTypeId, string childContentTypeId)
		{
			return
				!parentContentTypeId.IsNullOrEmpty() &&
				!childContentTypeId.IsNullOrEmpty() &&
				childContentTypeId.StartsWith(SystemPrefix) &&
				childContentTypeId.StartsWith(parentContentTypeId, StringComparison.OrdinalIgnoreCase);
		}

		public static bool IsChildContentTypeOf(this string childContentTypeId, string parentContentTypeId)
		{
			return
				!parentContentTypeId.IsNullOrEmpty() &&
				!childContentTypeId.IsNullOrEmpty() &&
				childContentTypeId.StartsWith(SystemPrefix) &&
				childContentTypeId.StartsWith(parentContentTypeId, StringComparison.OrdinalIgnoreCase);
		}

		/// <summary>
		/// Checks whether <paramref name="child"/> is child of or equals to <paramref name="parent"/>
		/// </summary>
		public static bool IsChildOrEqual(this SPContentTypeId child, SPContentTypeId parent)
		{
			return
				child == parent || child.IsChildOf(parent);
		}
	}
}